package com.voxel.sense.infomsg.widget;

import android.text.TextUtils;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.AppUtils;

import org.greenrobot.eventbus.EventBus;

import cyy.example.com.facotry.constants.CommonConstants;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.UpVersionEntity;
import cyy.example.com.facotry.net.helper.NetHelper;
import cyy.example.com.facotry.share.SPEngine;

/**
 * Created by ChenYiYao on 2018/10/11.
 * des : 检查版本更新管理器
 */
public class AppVersionManager {
    /**
     * 是否发送显示更新窗口消息
     *
     * @param isShowUp
     */
    public static void checkVerson(boolean isShowUp) {
        checkVerson(isShowUp, null);
    }

    public static void checkVerson(final boolean isShowUp, final DataSource.Callback callback) {
        NetHelper.checkAppVersion(new DataSource.Callback<BaseEntity<UpVersionEntity>>() {
            @Override
            public void onDataNotAvailable(String strRes, int reqId) {
                EventBus.getDefault().post(new EventEntity(EventBusConstants.APP_VERSION_CHECK, EventBusConstants.FAILURE));
                if (callback != null) {
                    callback.onDataNotAvailable(strRes, reqId);
                }
            }

            @Override
            public void onDataLoaded(BaseEntity<UpVersionEntity> entity, int reqId) {
                UpVersionEntity data = entity.getData();
                int appVersionCode = AppUtils.getAppVersionCode();

                if (data == null || TextUtils.isEmpty(data.getVerNumber())) {
                    //数据实体为空，则代表无更新
                    noNewVersion();
                } else if (Integer.parseInt(data.getVerNumber()) < appVersionCode) {
                    //从服务器获取的版本比本地的还低，这时也无需更新
                    noNewVersion();
                } else if (!TextUtils.isEmpty(data.getVerUrl())) {
                    notifyUpNewVerson(data, isShowUp);
                }
                if (callback != null) {
                    callback.onDataLoaded(entity, reqId);
                }
            }
        });
    }

    private static void noNewVersion() {
        SPEngine.getSPEngine().setAppUrl("");
        SPEngine.getSPEngine().setNewAppVersionNo(AppUtils.getAppVersionCode() + "");
        SPEngine.getSPEngine().setHaveNewApp(false);
        SPEngine.getSPEngine().setForceUp(false);
        EventBus.getDefault().post(new EventEntity(EventBusConstants.APP_VERSION_CHECK, EventBusConstants.SUCCEED));
    }

    private static void notifyUpNewVerson(UpVersionEntity data, boolean isShowUp) {
        String verNumber = data.getVerNumber();
        SPEngine.getSPEngine().setAppUrl(CommonConstants.FILE_CONFIG_URL + data.getVerUrl());
        SPEngine.getSPEngine().setNewAppVersionNo(verNumber);
        SPEngine.getSPEngine().setHaveNewApp(true);
        //发送消息通知更新
        if ("1".equals(data.getVerIsforce() + "")) {
            //强制更新时，统一发送弹窗消息
            SPEngine.getSPEngine().setForceUp(true);
            EventBus.getDefault().post(new EventEntity(EventBusConstants.SHOW_VERSION_UP_WINDOW, EventBusConstants.SUCCEED));
        } else if (isShowUp) {
            EventBus.getDefault().post(new EventEntity(EventBusConstants.SHOW_VERSION_UP_WINDOW, EventBusConstants.SUCCEED));
        } else {
            //通知有版本更新，但不需要弹窗
            EventBus.getDefault().post(new EventEntity(EventBusConstants.APP_VERSION_CHECK, EventBusConstants.SUCCEED));
        }
    }

    public static boolean haveNewVersion() {
        boolean flag = false;
        String newAppVersionNo = SPEngine.getSPEngine().getNewAppVersionNo();
        int appVersionCode = AppUtils.getAppVersionCode();
        boolean haveNewApp = SPEngine.getSPEngine().isHaveNewApp();
        /**
         * 判断是否有更新需要同时满足三个条件
         * 1. 有更新标记
         * 2. 有新版本的版本号
         * 3. 新的版本号大于本地的版本号
         */
        if (haveNewApp && !TextUtils.isEmpty(newAppVersionNo) && (newAppVersionNo.compareTo(appVersionCode + "") > 0)) {
            flag = true;
        } else if (TextUtils.isEmpty(newAppVersionNo)) {
            //版本号为空，则没有从服务器端获取到版本号
            noNewVersion();
            checkVerson(false);
        } else if (newAppVersionNo.equals(appVersionCode + "")) {
            //服务器获取的版本号跟本地一样
            noNewVersion();
        }
        return flag;
    }
}
